package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntConsumer;

public class FizzBuzz {
    private int n;
    private final Lock lock = new ReentrantLock();
    private final Condition a = lock.newCondition(),
                            b = lock.newCondition(),
                            c = lock.newCondition(),
                            d = lock.newCondition();

    public FizzBuzz(int n) {
        this.n = n;
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            lock.lock();
            if (i > 1) {
                a.await();
            }
            try {
                if (i % 3 == 0 && i % 5 != 0) {
                    printFizz.run();
                }
                b.signal();
            } catch (Exception e) {
            }finally {
                lock.unlock();
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            lock.lock();
            b.await();
            try {
                if (i % 3 == 0 && i % 5 != 0) {
                    printBuzz.run();
                }
                c.signal();
            } catch (Exception e) {
            }finally {
                lock.unlock();
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            lock.lock();
            c.await();
            try {
                if (i % 3 == 0 && i % 5 != 0) {
                    printFizzBuzz.run();
                }
                d.signal();
            } catch (Exception e) {
            }finally {
                lock.unlock();
            }
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            lock.lock();
            d.await();
            try {
                if (i % 3 == 0 && i % 5 != 0) {
                    printNumber.accept(i);
                }
                a.signal();
            } catch (Exception e) {
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        FizzBuzz fb = new FizzBuzz(15);
        new Thread(() -> {
            try {
                fb.fizz(() -> System.out.println("fizz"));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                fb.buzz(() -> System.out.println("buzz"));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                fb.fizzbuzz(() -> System.out.println("fizzbuzz"));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                fb.number(i -> System.out.println(i));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }).start();
    }
}
